//
//  EBMRequest.m
//  GuTouBao
//
//  Created by zhaoshijie on 2018/5/11.
//  Copyright © 2018 zhaoshijie. All rights reserved.
//

#import "EBMRequest.h"
#import "AFNetworking.h"
#import "EBMThirdPartyConfigure.h"
#import "SJDataManageTool.h"
//#import "EBMIMSingle.h"



@implementation EBMRequest


/**
 GET请求  返回数据不做处理直接返回
 
 @param urlString 接口地址
 @param headerDic 请求头
 @param parameters 内容
 @param successBlock 请求成功
 @param failBlock 请求失败
 */
+ (void)GETRequestWithUrlString:(NSString *)urlString
                      headerDic:(NSDictionary *)headerDic
                     parameters:(NSDictionary *)parameters
                   successBlock:(void(^)(id result))successBlock
                      failBlock:(void(^)(void))failBlock
{
    
    //  获取SessionManager请求对象
    AFHTTPSessionManager *manager = [self gainSessionManager];
    
    //  请求之前的准备
    [self requestBeforeConfigureWithManager:manager
                                  headerDic:headerDic
                                   loadView:nil
                                  tableView:nil];
    
    //  开始请求
    [manager GET:urlString parameters:parameters headers:headerDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *result = [SJDataManageTool jsonDataConvertToJosnClassWithJsonData:responseObject];
        
        
        if (successBlock) {
            successBlock(result) ;
        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if(failBlock)
        {
            failBlock();
        }
        
        
        //  加载网路错误的地址
        [self loadNetworkErrorWithUrlString:urlString];
        
        
        
        
        
    }] ;
}




/**
 GET请求  提取data数据    自定义加载框
 
 @param urlString 接口地址
 @param headerDic 请求头
 @param parameters 内容
 @param loadView 加载位置
 @param tableView 列表
 @param successBlock 请求成功
 @param serverErrorBlock 服务器错误
 @param failBlock 请求失败
 */
+ (void)GETRequestTakeDataWithUrlString:(NSString *)urlString
                              headerDic:(NSDictionary *)headerDic
                             parameters:(NSDictionary *)parameters
                               loadView:(UIView *)loadView
                              tableView:(EBMBaseTableView *)tableView
                           successBlock:(void(^)(id result))successBlock
                       serverErrorBlock:(void(^)(id result))serverErrorBlock
                              failBlock:(void(^)(void))failBlock
{
    
    
    //  获取SessionManager请求对象
    AFHTTPSessionManager *manager = [self gainSessionManager];

    //  请求之前的准备
    [self requestBeforeConfigureWithManager:manager
                                  headerDic:headerDic
                                   loadView:loadView
                                  tableView:tableView];
    
    //  开始请求
    [manager GET:urlString parameters:parameters headers:headerDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    
        
        //  请求成功返回数据的处理
        [self requestSuccessResponseHandleWithResponseObject:responseObject
                                                   responses:(NSHTTPURLResponse *)task.response
                                                    dataType:0
                                                    loadView:loadView
                                                   tableView:tableView
                                                   urlString:urlString
                                                successBlock:successBlock
                                            serverErrorBlock:serverErrorBlock
                                                   failBlock:failBlock];
      
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        //  请求失败返回数据的处理
        [self requestfailResponseHandleWithUrlString:urlString
                                            loadView:loadView
                                           tableView:tableView
                                            response:task.response
                                               error:error
                                           failBlock:failBlock];
        
        
        
    }] ;
}



/**
POST请求  query       自定义加载框  不提取data，用户登录、单独处理

@param urlString 接口地址
@param headerDic 请求头
@param parameters 内容
@param loadView 加载位置
@param successBlock 请求成功
@param serverErrorBlock 服务器错误
@param failBlock 请求失败
*/
+ (void)POSTRequestNoDataWithUrlString:(NSString *)urlString
                             headerDic:(NSDictionary *)headerDic
                            parameters:(NSDictionary *)parameters
                              loadView:(UIView *)loadView
                          successBlock:(void(^)(id result))successBlock
                      serverErrorBlock:(void(^)(id result))serverErrorBlock
                             failBlock:(void(^)(void))failBlock
{

    
    
    //  获取SessionManager请求对象
    AFHTTPSessionManager *manager = [self gainSessionManager];
    
    //  请求之前的准备
    [self requestBeforeConfigureWithManager:manager
                                  headerDic:headerDic
                                   loadView:loadView
                                  tableView:nil];
    
    
    //  开始请求
    [manager POST:urlString parameters:parameters headers:headerDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if(loadView)
        {
            [MBProgressHUDInLoading MBProgressHUDInStopWithView:loadView];
        }
        
        NSDictionary *result = [SJDataManageTool jsonDataConvertToJosnClassWithJsonData:responseObject];
        
        if (successBlock)
        {
            successBlock(result) ;
        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
        NSHTTPURLResponse * responses = (NSHTTPURLResponse *)task.response;
        
        if(loadView)
        {
            [MBProgressHUDInLoading MBProgressHUDInStopWithView:loadView];
        }
        
        NSInteger statusCode = responses.statusCode;
        
        if(statusCode >= 400 && statusCode<500)
        {
            //  特殊处理，后台说401也要接口正确
            
            NSData *errorData = [error.userInfo objectForKey:AFNetworkingOperationFailingURLResponseDataErrorKey];

            NSDictionary *result = [SJDataManageTool jsonDataConvertToJosnClassWithJsonData:errorData];
            
            NSInteger code = [result[@"code"] integerValue];
            
            NSString *errorString = result[@"error"];
            
            if(code == 500 || code == 400001 || code == 401001 || code == 401014 || [errorString isEqualToString:@"Unauthorized"])
            {
                if(serverErrorBlock)
                {
                    serverErrorBlock(result);
                }
                else
                {
                    [MBProgressHUDInLoading MBProgressHUDInTitleWithTitle:HttpUrlResultMessageError];
                }
                
            }
            else
            {
                [MBProgressHUDInLoading MBProgressHUDInTitleWithTitle:HttpUrlResultMessageError];
            }

        }
        else
        {
            
            if(failBlock)
            {
                failBlock();
            }
            
            //  加载网路错误的地址
            [self loadNetworkErrorWithUrlString:urlString];
        }
        
        
    }] ;
     
    
    
    
}




/**
POST请求  query       自定义加载框

@param urlString 接口地址
@param headerDic 请求头
@param parameters 内容
@param loadView 加载位置
@param tableView 列表
@param successBlock 请求成功
@param serverErrorBlock 服务器错误
@param failBlock 请求失败
*/
+ (void)POSTRequestWithUrlString:(NSString *)urlString
                       headerDic:(NSDictionary *)headerDic
                      parameters:(NSDictionary *)parameters
                        loadView:(UIView *)loadView
                       tableView:(EBMBaseTableView *)tableView
                    successBlock:(void(^)(id result))successBlock
                serverErrorBlock:(void(^)(id result))serverErrorBlock
                       failBlock:(void(^)(void))failBlock
{

    
    
    //  获取SessionManager请求对象
    AFHTTPSessionManager *manager = [self gainSessionManager];
    
    //  请求之前的准备
    [self requestBeforeConfigureWithManager:manager
                                  headerDic:headerDic
                                   loadView:loadView
                                  tableView:tableView];
    
    
    //  开始请求
    [manager POST:urlString parameters:parameters headers:headerDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        //  请求成功返回数据的处理
        [self requestSuccessResponseHandleWithResponseObject:responseObject
                                                   responses:(NSHTTPURLResponse *)task.response
                                                    dataType:0
                                                    loadView:loadView
                                                   tableView:tableView
                                                   urlString:urlString
                                                successBlock:successBlock
                                            serverErrorBlock:serverErrorBlock
                                                   failBlock:failBlock];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
        //  请求失败返回数据的处理
        [self requestfailResponseHandleWithUrlString:urlString
                                            loadView:loadView
                                           tableView:tableView
                                            response:task.response
                                               error:error
                                           failBlock:failBlock];
        
        
    }] ;
     
    
    
    
}



/**
 POST请求 body    自定义加载框
 
 @param urlString 接口地址
 @param headerDic 请求头
 @param parameters 内容
 @param loadView 内容
 @param tableView 列表
 @param successBlock 请求成功
 @param serverErrorBlock 服务器错误
 @param failBlock 请求失败
 */
+ (void)POSTRequestInDataTaskWithUrlString:(NSString *)urlString
                                 headerDic:(NSDictionary *)headerDic
                                parameters:(NSDictionary *)parameters
                                  loadView:(UIView *)loadView
                                 tableView:(EBMBaseTableView *)tableView
                              successBlock:(void(^)(id result))successBlock
                          serverErrorBlock:(void(^)(id result))serverErrorBlock
                                 failBlock:(void(^)(void))failBlock
{
    
    
    NSDictionary *body = parameters;

    NSError *error;

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:body options:0 error:&error];

    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    

    NSMutableURLRequest *req = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:urlString parameters:nil error:nil];

    req.timeoutInterval= [[[NSUserDefaults standardUserDefaults] valueForKey:@"timeoutInterval"] longValue];

    [req setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    
    //  加鉴权字段
    headerDic = [self addAuthenticationParameterHeaderDic:headerDic];
    
    for (NSString *key in headerDic)
    {
        //  设置请求头
        [req setValue:headerDic[key] forHTTPHeaderField:key];
    }

    [req setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
    
    
    //  加载框
    if(loadView)
    {
        [MBProgressHUDInLoading MBProgressHUDInLoadingWithView:loadView];
    }
    
    //  列表
    if(tableView)
    {
        [tableView reloadEmptyDataSetInDispatchInRqeust];
    }
    
    
    [[manager dataTaskWithRequest:req uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        
        
        
        if (!error)
        {

            if ([responseObject isKindOfClass:[NSDictionary class]])
            {
                
                //  请求成功返回数据的处理
                [self requestSuccessResponseHandleWithResponseObject:responseObject
                                                           responses:(NSHTTPURLResponse *)response
                                                            dataType:1
                                                            loadView:loadView
                                                           tableView:tableView
                                                           urlString:urlString
                                                        successBlock:successBlock
                                                    serverErrorBlock:serverErrorBlock
                                                           failBlock:failBlock];

            }

        } else {
            
            if ([responseObject isKindOfClass:[NSDictionary class]])
            {
                NSDictionary *result = responseObject;
                NSString *error = result[@"error"];
                
                if([error isEqualToString:@"invalid_token"])
                {
                    //  token失效
                    //  清楚用户信息
                    [KUserSinge removeUserInfomation];
                    
                    
                    if(loadView)
                    {
                        [MBProgressHUDInLoading MBProgressHUDInStopWithView:loadView];
                    }
                    
                    if(failBlock)
                    {
                        failBlock();
                    }

                    return;
                    
                }
            }
            
            
            //  请求失败返回数据的处理
            [self requestfailResponseHandleWithUrlString:urlString
                                                loadView:loadView
                                               tableView:tableView
                                                response:responseObject
                                                   error:error
                                               failBlock:failBlock];

            
        
        }

    }] resume];
    
}



/**
 POST请求
 
 @param urlString 接口地址
 @param headerDic 请求头
 @param parameters 内容
 @param imagesArray 图片
 @param loadView 加载位置
 @param successBlock 请求成功
 @param serverErrorBlock 请求成功
 @param failBlock 请求失败
 */
+ (void)POSTRequestUploadImageWithUrlString:(NSString *)urlString
                                  headerDic:(NSDictionary *)headerDic
                                 parameters:(NSDictionary *)parameters
                                imagesArray:(NSArray *)imagesArray
                                   loadView:(UIView *)loadView
                               successBlock:(void(^)(id result))successBlock
                           serverErrorBlock:(void(^)(id result))serverErrorBlock
                                  failBlock:(void(^)(void))failBlock
{
    
    //  获取SessionManager请求对象
    AFHTTPSessionManager *manager = [self gainSessionManager];
    
    //  请求之前的准备
    [self requestBeforeConfigureWithManager:manager
                                  headerDic:headerDic
                                   loadView:loadView
                                  tableView:nil];
    
    
    //  开始请求
    [manager POST:urlString parameters:parameters headers:headerDic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        //  设置图片
        for (NSInteger i=0 ; i<imagesArray.count ; i++)
        {
            UIImage *image = imagesArray[i];
            
            NSData *imageData = UIImageJPEGRepresentation(image, 0.01) ;
            
            //  获取时间戳
            NSString *timeStampString = @((NSInteger)[SJDateConvertTool getThePresentTimeStamp]).stringValue;
            
            //  设置图片名称
            NSString *imageName = [NSString stringWithFormat:@"%@_%@_iOS.jpg",timeStampString,@(i)];
            NSString *name = @"file";
            
            [formData appendPartWithFileData:imageData name:name fileName:imageName mimeType:@"image/jpg"];
        }
        
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        //  请求成功返回数据的处理
        [self requestSuccessResponseHandleWithResponseObject:responseObject
                                                   responses:(NSHTTPURLResponse *)task.response
                                                    dataType:0
                                                    loadView:loadView
                                                   tableView:nil
                                                   urlString:urlString
                                                successBlock:successBlock
                                            serverErrorBlock:serverErrorBlock
                                                   failBlock:failBlock];
    
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        //  请求失败返回数据的处理
        [self requestfailResponseHandleWithUrlString:urlString
                                            loadView:loadView
                                           tableView:nil
                                            response:task.response
                                               error:error
                                           failBlock:failBlock];
        
    }] ;
}



/// 获取SessionManager请求对象
+ (AFHTTPSessionManager *)gainSessionManager
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager] ;
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringCacheData;
    manager.requestSerializer.timeoutInterval = RequestWaitTimeLong;
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/plain",@"application/json",@"text/json",@"text/javascript",@"text/html",@"application/javascript", nil]];
    
    return manager;
}



/// 添加鉴权字段
/// @param headerDic 请求头字段
+ (NSDictionary *)addAuthenticationParameterHeaderDic:(NSDictionary *)headerDic
{
    NSMutableDictionary *dic_m ;
    
    if(headerDic)
    {
        dic_m = [[NSMutableDictionary alloc]initWithDictionary:headerDic];
    }
    else
    {
        dic_m = [[NSMutableDictionary alloc]init];
    }
    
    //  添加公共参数
    
    NSString *terminalType = @"10";
    NSString *softVersion = [SJAppInformationTool gainApplicationVersion];
    NSString *softName = [SJAppInformationTool gainApplicationName];
    NSString *MAC = [SJAppInformationTool gainMobileMACAddress];
    NSString *IDFV = [SJAppInformationTool gainMobileIDFV];
    NSString *OSV = [SJAppInformationTool gainMobileSystemModel];
    
    
    NSArray *common_header_array = @[[NSString stringWithFormat:@"terminalType=%@",terminalType],
                                     [NSString stringWithFormat:@"softVersion=%@",softVersion],
                                     [NSString stringWithFormat:@"softName=%@",softName],
                                     [NSString stringWithFormat:@"MAC=%@",MAC],
                                     [NSString stringWithFormat:@"IDFV=%@",IDFV],
                                     [NSString stringWithFormat:@"OSV=%@",OSV]
    ];
    
    NSString *common_header = [common_header_array componentsJoinedByString:@"&"];
    
    [dic_m setObject:common_header forKey:@"TerminalInfo"];
  
    
    if(KUserSingeIsLogin)
    {
//        [dic_m setObject:@"abcdef-ghjjsjf-hifoaa-oppio" forKey:@"token"];

        //  微信小程序   外网地址
        [dic_m setObject:KUserSinge.token forKey:@"Authorization"];
        
        
        
    }
    else
    {
        //  用户的短信验证码登录需要的验证数据
//        [dic_m setObject:@"Basic ZWJvbmQtd3htcDp7bm9vcH1zZWNyZXQ=" forKey:@"Authorization"];
    }
      
    //  外网
//    [dic_m setObject:@"bearer 8e714e64-f731-4942-817a-70dc613b138c" forKey:@"Authorization"];
    
//    [dic_m setObject:@"Bearer 9bf7e0ea-de4e-4b38-9e2d-89b9c76a0f5a" forKey:@"Authorization"];
    
    
    
    
    
    
    
    return dic_m;
}




/// 请求之前的准备
/// @param manager 请求对象
/// @param headerDic 请求头
/// @param loadView 加载试图
/// @param tableView 列表
+ (void)requestBeforeConfigureWithManager:(AFHTTPSessionManager *)manager
                                headerDic:(NSDictionary *)headerDic
                                 loadView:(UIView *)loadView
                                tableView:(EBMBaseTableView *)tableView
{
    
    
    //  加鉴权字段
    NSDictionary *header_dic = [self addAuthenticationParameterHeaderDic:headerDic];
    
    for (NSString *key in header_dic)
    {
        //  设置请求头
        [manager.requestSerializer setValue:header_dic[key] forHTTPHeaderField:key];
    }
    
    //  加载框
    if(loadView)
    {
        [MBProgressHUDInLoading MBProgressHUDInLoadingWithView:loadView];
    }
    
    //  列表
    if(tableView)
    {
        [tableView reloadEmptyDataSetInDispatchInRqeust];
    }
    
    
    
}




/// 请求成功返回数据的处理
/// @param responseObject 返回数据
/// @param responses 返回响应
/// @param dataType 返回数据是否需要转码
/// @param loadView 加载提示框的背景视图
/// @param tableView 列表
/// @param urlString 接口地址
/// @param successBlock 请求成功
/// @param serverErrorBlock 服务器错误
/// @param failBlock 请求失败

+ (void)requestSuccessResponseHandleWithResponseObject:(id)responseObject
                                             responses:(NSHTTPURLResponse *)responses
                                              dataType:(NSInteger)dataType
                                              loadView:(UIView *)loadView
                                             tableView:(EBMBaseTableView *)tableView
                                             urlString:(NSString *)urlString
                                          successBlock:(void(^)(id result))successBlock
                                      serverErrorBlock:(void(^)(id result))serverErrorBlock
                                             failBlock:(void(^)(void))failBlock
{
    
    if(loadView)
    {
        [MBProgressHUDInLoading MBProgressHUDInStopWithView:loadView];
    }
    
    if(tableView)
    {
        [tableView baseReloadData];
    }
    
    NSInteger statusCode = responses.statusCode;
    
    if(statusCode == 401)
    {
        //  token失效
        //  清楚用户信息
        
        [KUserSinge removeUserInfomation];
        
        if(failBlock)
        {
            failBlock();
        }
        
        [MBProgressHUDInLoading MBProgressHUDInTitleWithTitle:@"请重新登录"];
        
    }
    else
    {
        NSDictionary *result = responseObject;
        
        if(dataType == 0)
        {
            result = [SJDataManageTool jsonDataConvertToJosnClassWithJsonData:responseObject];
        }

        NSInteger code = [result[@"code"] integerValue];

        if(code == 100000)
        {

            NSDictionary *data_dic = result[@"data"];

            //  请求响应数据对积分结构的处理过滤
            data_dic = [self responseDataHandleIntegralWithResult:data_dic];

            if (successBlock)
            {
                successBlock(data_dic) ;
            }

        }
        else
        {
            //  服务器错误
            
            //  token失效的检测
            if([self userTokenInvalidInResult:result
                                    urlString:urlString
                                    failBlock:failBlock])
            {
                return;
            }
            
            if(serverErrorBlock)
            {
                serverErrorBlock(result);
            }
            else
            {
                [MBProgressHUDInLoading MBProgressHUDInTitleWithTitle:HttpUrlResultMessageError];
            }
            
        }
    }
    
}



/// 用户code失效
/// @param result 包含code的返回数据
/// @param urlString 接口地址
/// @param failBlock 请求失败
+ (BOOL)userTokenInvalidInResult:(NSDictionary *)result
                       urlString:urlString
                       failBlock:(void(^)(void))failBlock
{
    
    
    NSString *error = result[@"error"];
    
    if([error isEqualToString:@"invalid_token"] || [error isEqualToString:@"unauthorized"])
    {
        //  token失效
        //  清楚用户信息
        
        [KUserSinge removeUserInfomation];
        
        //  退出im账号
//        [[EBMIMSingle shareIMSingle]logoutIMUser];
        
        if(failBlock)
        {
            failBlock();
        }
        
//        [MBProgressHUDInLoading MBProgressHUDInTitleWithTitle:@"请重新登录"];
        
        return YES;
        
    }
    
    return NO;
    
}



/// 请求失败返回数据的处理
/// @param urlString 请求地址
/// @param loadView 加载提示框的背景视图
/// @param tableView 列表
/// @param response 请求响应
/// @param error 错误信息
/// @param failBlock 请求失败
+ (void)requestfailResponseHandleWithUrlString:(NSString *)urlString
                                      loadView:(UIView *)loadView
                                     tableView:(EBMBaseTableView *)tableView
                                      response:(NSURLResponse *)response
                                         error:(NSError *)error
                                     failBlock:(void(^)(void))failBlock
{
    if(loadView)
    {
        [MBProgressHUDInLoading MBProgressHUDInStopWithView:loadView];
    }
    
    if(tableView)
    {
        [tableView baseReloadData];
    }
    
    
    if ([response isKindOfClass:[NSDictionary class]])
    {
        NSDictionary *result = (NSDictionary *)response;
        NSString *error = result[@"error"];
        
        if([error isEqualToString:@"invalid_token"])
        {
            //  token失效
            //  清楚用户信息
            [KUserSinge removeUserInfomation];
            
            //  退出im账号
//            [[EBMIMSingle shareIMSingle]logoutIMUser];
            
            
            if(loadView)
            {
                [MBProgressHUDInLoading MBProgressHUDInStopWithView:loadView];
            }
            
            if(failBlock)
            {
                failBlock();
            }

            return;
            
        }
    }
    else
    {
        NSHTTPURLResponse *responses = (NSHTTPURLResponse *)response;
        
        NSInteger statusCode = responses.statusCode;
        
        if(statusCode >= 400 && statusCode<500)
        {
            //  特殊处理，后台说401也要接口正确
            
            NSData *errorData = [error.userInfo objectForKey:AFNetworkingOperationFailingURLResponseDataErrorKey];

            NSDictionary *result = [SJDataManageTool jsonDataConvertToJosnClassWithJsonData:errorData];
            
            
            //  token失效的检测
            if([self userTokenInvalidInResult:result
                                    urlString:urlString
                                    failBlock:failBlock])
            {
                return;
            }

        }
        
        
        if(statusCode == 500)
        {
            //  设置密码需要这样操作一下，错误信息从500中获取。
            
            NSData *errorData = [error.userInfo objectForKey:AFNetworkingOperationFailingURLResponseDataErrorKey];
            
            if(error)
            {
                NSDictionary *result = [SJDataManageTool jsonDataConvertToJosnClassWithJsonData:errorData];
                
                if([result isKindOfClass:[NSDictionary class]])
                {
                    NSString *message = result[@"message"];
                    
                    if(message)
                    {
                        [MBProgressHUDInLoading MBProgressHUDInTitleWithTitle:message];
                        
                        return;
                    }
                }
            }

            
        }
    }
    
    
    if(failBlock)
    {
        failBlock();
    }
    
    //  加载网路错误的地址
    [self loadNetworkErrorWithUrlString:urlString];
    
}


/// 加载网路错误的地址
/// @param urlString 请求地址
+ (void)loadNetworkErrorWithUrlString:(NSString *)urlString
{

    
    NSString *message = HttpUrlError;
    
    
    if([HttpUrl_Main isEqualToString:@"http://ebondtest.fcsc.com/app-api"] || [HttpUrl_Main isEqualToString:@"http://10.224.201.240:8120/app-api"] )
    {
        
        message = [NSString stringWithFormat:@"%@,url=%@",HttpUrlError,urlString];
    }
    
    
    if(KDebugStatus)
    {
        [MBProgressHUDInLoading MBProgressHUDInTitleWithTitle:message];
    }
    
    
    
}


/// 请求响应数据对积分结构的处理过滤
/// @param result data数据
+ (id)responseDataHandleIntegralWithResult:(id)result
{
    if(result && [result isKindOfClass:[NSDictionary class]])
    {
        NSDictionary *integral = result[@"integral"];
        
        if(integral && [integral isKindOfClass:[NSDictionary class]])
        {
            id data = result[@"data"];
            
            //  需要显示积分的提示
            
            if([integral[@"flag"] integerValue] == 1)
            {
                NSInteger reward = [integral[@"reward"] integerValue];
                NSString *taskName = integral[@"taskName"];
                
                [MBProgressHUDInLoading MBProgressHUDInTitleWithTitle:[NSString stringWithFormat:@"%@\n+%@积分",taskName,@(reward).stringValue]];
            }
            
            if(data && ![integral isKindOfClass:[NSNull class]])
            {
                return data;
            }
            
        }
    }
    
    return result;
}





@end
